<?php
/**
 * Minhlog 文章列表页控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Frontend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: ListController.php 30 2010-03-14 06:35:04Z minh.shu $
 *
 */

class ListController extends Minh_Controller_Action
{
    protected $_articleModel;
    protected $_categoryModel;
    protected $_tagModel;

    public function init()
    {
        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        if(null === $this->_categoryModel){
            if(class_exists('Domain_Model_Category')){
                $this->_categoryModel = new Domain_Model_Category();
            } else {
                throw new Exception('文章分类模型类不存在!');
            }
        }

        if(null === $this->_tagModel){
            if(class_exists('Domain_Model_Tag')){
                $this->_tagModel = new Domain_Model_Tag();
            } else {
                throw new Exception('标签模型类不存在!');
            }
        }

        parent::init();
    }

    /**
     * 默认列表控制器
     */
    public function indexAction()
    {
        $filter = new Zend_Filter_Int();
        $page = $filter->filter($this->_getParam('page', 1));

        $cacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $page);
        if(! ($cacheValue = $this->cache->load($cacheId))) {
            $resultSet = $this->_articleModel->getAllArticle();

            if(null !== $resultSet){
                foreach($resultSet as $articleSet){
                    $articleSet->article->publishTime = date('Y-m-d', $articleSet->article->publishTime);
                }
                $perPageNum = 6;
                $articleList = Zend_Paginator::factory($resultSet);
                $articleList->setCurrentPageNumber($page)->setItemCountPerPage($perPageNum);
                $this->cache->save($articleList, $cacheId);
            }
        } else {
            $articleList = $cacheValue;
        }

        //SEO 信息
        if($page > 1) $this->view->headTitle()->append('Part '.$page);
        $this->view->headMeta()->appendName('keywords', $this->seo['list']['index']['keywords']);
        $this->view->headMeta()->appendName('description', $this->seo['list']['index']['description']);
        if(!empty($articleList)) $this->view->articleList = $articleList;

        $this->fetchExecuteTime();

        echo $this->view->render('list.phtml');
    }

    /**
     * 分类日志列表控制器
     */
    public function categoryAction()
    {
        $filter = new Zend_Filter_Int();
        $categoryId = $filter->filter($this->_getParam('id', 0));
        $page = $filter->filter($this->_getParam('page', 1));

        $cacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $categoryId . '_' . $page);
        if(! ($cacheValue = $this->cache->load($cacheId))) {
            $categorySet = $this->_categoryModel->getRelatedCategory($categoryId);
            $resultSet = $this->_articleModel->getArticleByCategory($categoryId, $categorySet);

            if(null === $resultSet){
                $this->_redirect('/index.html');
            } else {
                foreach($resultSet as $articleSet){
                    $articleSet->article->publishTime = date('Y-m-d', $articleSet->article->publishTime);
                }
                $perPageNum = 6;
                $articleList = Zend_Paginator::factory($resultSet);
                $articleList->setCurrentPageNumber($page)->setItemCountPerPage($perPageNum);
            }
            $this->cache->save($articleList, $cacheId);
        } else {
            $articleList = $cacheValue;
        }

        $categoryInfo = $this->_categoryModel->getCategoryById($categoryId);

        //SEO 信息
        if($page > 1) $this->view->headTitle()->append('Part '.$page);
        $this->view->headTitle($categoryInfo->categoryName, 'PREPEND');
        $this->view->headMeta()->appendName('keywords', $this->seo['list']['category']['keywords'] . ',' . $categoryInfo->categoryName);
        $this->view->headMeta()->appendName('description',$categoryInfo->categoryName.'分类下全部文章.'.$categoryInfo->categoryDesc . ' - Part '.$page);
        $this->view->articleList = $articleList;

        $this->fetchExecuteTime();

        echo $this->view->render('list.phtml');
    }

    /**
     * 标签日志列表控制器
     */
    public function tagAction()
    {
        $filter = new Zend_Filter_Int();
        $tagId = $filter->filter($this->_getParam('id', 0));
        $page = $filter->filter($this->_getParam('page', 1));

        $cacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $tagId . '_' . $page);
        if(! ($cacheValue = $this->cache->load($cacheId))) {
            $relationSet = $this->_tagModel->getRelationByTag($tagId);
            $resultSet = $this->_articleModel->getArticleByTag($relationSet);
            if(null === $resultSet){
                $this->_redirect('/index.html');
            } else {
                foreach($resultSet as $articleSet){
                    $articleSet->article->publishTime = date('Y-m-d', $articleSet->article->publishTime);
                }
                $perPageNum = 6;
                $articleList = Zend_Paginator::factory($resultSet);
                $articleList->setCurrentPageNumber($page)->setItemCountPerPage($perPageNum);
            }
            $this->cache->save($articleList, $cacheId);
        } else {
            $articleList = $cacheValue;
        }

        $tagInfo = $this->_tagModel->getTagById($tagId);

        //SEO 信息
        if($page > 1) $this->view->headTitle()->append('Part '.$page);
        $this->view->headTitle($tagInfo->tagName, 'PREPEND');
        $this->view->headMeta()->appendName('keywords', $this->seo['list']['tag']['keywords'] . ',' . $tagInfo->tagName);
        $this->view->headMeta()->appendName('description',$tagInfo->tagName.'标签下全部文章 - Part '.$page);
        $this->view->articleList = $articleList;

        $this->fetchExecuteTime();

        echo $this->view->render('list.phtml');
    }

    /**
     * 存档日志列表控制器
     */
    public function archiveAction()
    {
        $filter = new Zend_Filter_Int();
        $year = $filter->filter($this->_getParam('year', date('Y')));
        $month = $filter->filter($this->_getParam('month', date('m')));
        $page = $filter->filter($this->_getParam('page', 1));

        $cacheId = md5($this->_request->getControllerName() . '_' . $this->_request->getActionName() . '_' . $year . '_' . $month . '_' . $page);
        if(! ($cacheValue = $this->cache->load($cacheId))) {
            $resultSet = $this->_articleModel->getArticleByArchive($year, $month);
            if(null === $resultSet){
                $this->_redirect('/index.html');
            } else {
                foreach($resultSet as $articleSet){
                    $articleSet->article->publishTime = date('Y-m-d', $articleSet->article->publishTime);
                }
                $perPageNum = 6;
                $articleList = Zend_Paginator::factory($resultSet);
                $articleList->setCurrentPageNumber($page)->setItemCountPerPage($perPageNum);
            }
            $this->cache->save($articleList, $cacheId);
        } else {
            $articleList = $cacheValue;
        }

        //SEO 信息
        if($page > 1) $this->view->headTitle()->append('Part '.$page);
        $this->view->headTitle($year.'年'.$month.'月全部文章', 'PREPEND');
        $this->view->headMeta()->appendName('keywords', $this->seo['list']['tag']['keywords'] . ',' . $year . '年' . $month . '月');
        $this->view->headMeta()->appendName('description', $year . '年' . $month . '月全部文章归档页面' . ' - Part '.$page);
        $this->view->articleList = $articleList;

        $this->fetchExecuteTime();

        echo $this->view->render('list.phtml');
    }

    public function  __call($action,  $arguments) {
        if ('Action' == substr($action, -6)) {
            $this->_forward('index');
        }
    }
}
